//
//  Imprint.m
//  Speedater
//
//  Created by NgoThang on 12/30/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "Imprint.h"
#import "Start.h"
#import "AppDelegate.h"

@interface Imprint (Private)

- (void)cleanup;

@end

@implementation Imprint (Private)

- (void)cleanup
{
    self.lb1 = nil;
    self.lb2 = nil;
    self.lb3 = nil;
    self.lb4 = nil;
    self.lb5 = nil;
    self.lb6 = nil;
    self.lb7 = nil;
    self.lb8 = nil;
    self.lb9 = nil;
    self.myTabBar = nil;
}

@end


@implementation Imprint

@synthesize lb1, lb2, lb3, lb4, lb5, lb6, lb7, lb8, lb9;
@synthesize myTabBar;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    //NSLog(@"IMPRINT -> dealloc");
    [self cleanup];
    [super dealloc];
}

#pragma mark - Custom functions

- (void)goStart
{
    // goto Start form
    for (UIViewController *vc in self.navigationController.viewControllers)
        if ([vc isKindOfClass:[Start class]]){
            //NSLog(@"IMPRINT -> popToViewController: %@", vc);
            [self.navigationController popToViewController:vc animated:YES];
            return;
        }
}

- (void)goBack
{
    // goto login form
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    //NSLog(@"IMPRINT -> viewDidLoad");
    [super viewDidLoad];
    
    // navigation bar
    self.navigationController.navigationBar.hidden = NO;
    
    // background
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
    
    // navigation title
    self.title = @"Impressum";
    
    // navigation buttons
    UIButton *logoButton = [[UIButton alloc] initWithFrame:CGRectMake(0,2,35,35)];
    [logoButton setBackgroundImage:[UIImage imageNamed:@"logo.png"] forState:UIControlStateNormal];
    [logoButton addTarget:self action:@selector(goStart) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *iLButton = [[UIBarButtonItem alloc] initWithCustomView:logoButton];
    self.navigationItem.leftBarButtonItem = iLButton;
    [logoButton release];
    [iLButton release];
    
    UIButton *backButton = [UIButton buttonWithType:100];
    [backButton setTitle:@"Zurück" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *iRButton = [[UIBarButtonItem alloc] initWithCustomView: backButton];
    self.navigationItem.rightBarButtonItem = iRButton;
    [iRButton release];
    
    // update text
    [lb1 setText:NSLocalizedString(@"contact_txt_1", nil)];
    [lb2 setText:NSLocalizedString(@"contact_txt_2", nil)];
    [lb3 setText:NSLocalizedString(@"contact_txt_3", nil)];
    [lb4 setText:NSLocalizedString(@"contact_txt_4", nil)];
    [lb5 setText:NSLocalizedString(@"contact_txt_5", nil)];
    [lb6 setText:NSLocalizedString(@"contact_txt_6", nil)];
    [lb7 setText:NSLocalizedString(@"contact_txt_7", nil)];
    [lb8 setText:NSLocalizedString(@"contact_txt_8", nil)];
    [lb9 setText:NSLocalizedString(@"contact_txt_9", nil)];
    
    // check show or hide tabbar
    if ([[NSUserDefaults standardUserDefaults] stringForKey:@"username"])
        self.myTabBar.hidden = NO;
    else
        self.myTabBar.hidden = YES;
}

- (void)viewDidUnload
{
    //NSLog(@"IMPRINT -> viewDidUnload");
    [self cleanup];
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    //NSLog(@"IMPRINT -> viewWillAppear");
    //NSLog(@"IMPRINT -> viewControllers: %@", self.navigationController.viewControllers);
    
    // save state
    [AppDelegate saveState:0 withViewController:self];
    
    // update tabbar
    [AppDelegate updateTabBarItem:myTabBar withMailValue:-1 andUpdateMute:NO];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark TabBar

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    [myTabBar setSelectedItem:nil];
    switch (item.tag) {
        case 0:
            [[[[self.tabBarController.viewControllers objectAtIndex:1] topViewController] navigationController] popToRootViewControllerAnimated:NO];
            self.tabBarController.selectedIndex = 1;
            break;
        case 1:
            [[[[self.tabBarController.viewControllers objectAtIndex:2] topViewController] navigationController] popToRootViewControllerAnimated:NO];
            self.tabBarController.selectedIndex = 2;
            break;
        case 2: 
            [[[[self.tabBarController.viewControllers objectAtIndex:3] topViewController] navigationController] popToRootViewControllerAnimated:NO];
            self.tabBarController.selectedIndex = 3;
            break;
        case 3: 
            [AppDelegate updateTabBarItem:myTabBar withMailValue:-1 andUpdateMute:YES];
            break;
        default:
            break;
    }
}

@end
